package router

import (
	"github.com/gin-gonic/gin"
	"go-sec-code/database"
	"net/http"
)

type SqlInHandler struct {
}

func NewSqlInHandler() *SqlInHandler {
	return &SqlInHandler{}
}

func (handler *SqlInHandler) RegisterRoutes(r *gin.RouterGroup) {
	r.GET("/get_user", handler.GetUserList)
}

func (handler *SqlInHandler) GetUserList(ctx *gin.Context) {
	name := ctx.Query("name")
	orderField := ctx.Query("orderField")
	//pageNo, _ := strconv.Atoi(ctx.Query("pageNo"))
	//pageSize, _ := strconv.Atoi(ctx.Query("pageSize"))

	//mysqlUser := database.NewMysqlUser()
	//db, _ := mysqlUser.ConnectDB()
	//users := mysqlUser.SelectByName(db, name, orderField)

	//gormUser := database.NewGormUser()
	//db := gormUser.GetDB()
	//users := gormUser.SelectListByName(db, name, orderField)

	xormUser := database.NewXormUser()
	engine := xormUser.GetEngine()
	users := xormUser.SelectUsersByName(engine, name, orderField)
	//limit := pageSize;
	//offset := (pageNo - 1) * pageSize;
	//users := xormUser.SelectUsersLimit(engine, limit, offset)

	ctx.JSON(http.StatusOK, users)
}

